//
//  HomeNaviView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/20.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "HomeNaviView.h"

#import "CustomHomeNaviButton.h"



#import "PullingRefreshView.h"//refreshView

//data
#import "BCHomeFlowModel.h"

@interface HomeNaviView ()

@property (nonatomic, strong) UIView *backgroundView;

/**
 *contentView
 */
@property (nonatomic, strong) UIView *contentView;

/**
 *PullingRefreshView
 */
@property (nonatomic, strong) PullingRefreshView *refreshView;

///**
// *regionButton地区
// */
//@property (nonatomic, strong) QMUILinkButton *regionButton;

/**
 *appNameLabel
 */
@property (nonatomic, strong) UILabel *appNameLabel;

/**
 *serviceButton
 */
@property (nonatomic, strong) CustomHomeNaviButton *serviceButton;
/**
 *prizeButton
 */
@property (nonatomic, strong) CustomHomeNaviButton *prizeButton;

#pragma mark - data
/**
 *maxHeight
 */
@property (nonatomic, assign) CGFloat maxHeight;
/**
 *minHeight
 */
@property (nonatomic, assign) CGFloat minHeight;

@end

@implementation HomeNaviView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.clipsToBounds = true;
        self.maxHeight = [self maxHeight];
        self.minHeight = [self minHeight];
        [self setupSubviews];
        
        [self displaySubviews:false];
    }
    return self;
}
- (void)setupSubviews {
    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    self.backgroundView.alpha = 0;
    //
    self.contentView = [UIView new];
//    self.contentView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.top.mas_equalTo(Height_StatusBar);
    }];
    //
    [self setContents];
    //
    self.refreshView = [PullingRefreshView new];
    [self addSubview:self.refreshView];
    [self.refreshView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(44.0);
    }];
}
- (void)setContents {
    //
    self.serviceButton = [CustomHomeNaviButton new];
    [self.serviceButton setImage:LOAD_IMAGE(@"home_navi_service") forState:UIControlStateNormal];
    //    self.serviceButton.customTitleLabel.text = BCLocalizedKey(@"home_navi_service");
    [self.contentView addSubview:self.serviceButton];
    //
    self.prizeButton = [CustomHomeNaviButton new];
    [self.prizeButton setImage:LOAD_IMAGE(@"home_navi_prize") forState:UIControlStateNormal];
    //    self.prizeButton.customTitleLabel.text = BCLocalizedKey(@"home_navi_prize");
    [self.contentView addSubview:self.prizeButton];
    //
    [self.serviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-5.0);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.prizeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.serviceButton.mas_left).offset(-15.0);
        make.centerY.mas_equalTo(self.contentView);
    }];
    //
    CGSize iconSize = [self iconSize];
    self.iconImageView = [HomeNaviIconImageView new];
    //    self.iconImageView.image = LOAD_IMAGE(@"home_navi_logo");
    [self.contentView addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(10.0);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(iconSize);
    }];
    
    //
    self.regionButton = [QMUILinkButton new];
    //    [self.regionButton setTitle:@"亚太区" forState:UIControlStateNormal];
    [self.regionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.regionButton.titleLabel.font = [UIFont systemFontOfSize:25 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.regionButton];
    [self.regionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(10.0);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.regionButton.titleLabel.font.lineHeight);
    }];
    
    //
    self.appNameLabel = [UILabel new];
    self.appNameLabel.textColor = RGBHEXA(0xFFFFFF, 0.8);
    self.appNameLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.appNameLabel];
    [self.appNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.regionButton);
        make.bottom.mas_equalTo(self.regionButton.mas_top).offset(-3.0);
    }];
}

/**
 pullingPercent 设置刷新时的pullingPercent 和refreshHeader同步
 */
- (void)setPullingPercent:(CGFloat)pullingPercent contentOffset:(CGPoint)contentOffset {
    //
    if (self.flowData) {//没得数据何来刷新
        if (contentOffset.y>=0) {
            pullingPercent = 0;
        }
        
        self.contentView.alpha = 1-pullingPercent;
        //
        [self.refreshView updatePullingPercent:pullingPercent];
    }
}
- (void)beginRefreshing {
    if (self.flowData) {
        self.contentView.alpha = 0;
        [self.refreshView beginRefreshing];
    }
}
- (void)endRefreshing {
    if (self.flowData) {
        self.contentView.alpha = 1;
        [self.refreshView endRefreshing];
    }
}


- (void)layoutSubviewsWithTableViewContentOffset:(CGPoint)contentOffset {
    //
    [self updateSelfLayoutContentOffset:contentOffset];
}
/**
 改变视图透明状态和自身frame的变化
 */
- (void)updateSelfLayoutContentOffset:(CGPoint)contentOffset {
    //自身高度
    CGFloat height = 0;
    CGFloat maxHeight = self.maxHeight;
    CGFloat minHeight = self.minHeight;
    CGFloat maxContentOffsetY = [self maxContentOffsetY];
    //
    CGFloat bili = 0;
    //
    CGFloat topOffset = 0;
    if (contentOffset.y<=0) {//初始化状态
        height = maxHeight;
        //
        bili = 0;
        //        topOffset -= contentOffset.y;
    }else if (contentOffset.y>=maxContentOffsetY) {//navi顶部状态
        height = minHeight;
        //
        bili = 1;
    }else {//渐变状态
        CGFloat changeValueTotle = self.maxHeight-self.minHeight;
        CGFloat offsetBili = contentOffset.y/maxContentOffsetY;
        height = self.maxHeight-changeValueTotle*offsetBili;
        //
        bili = offsetBili;
    }
    //
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.superview).offset(topOffset);
        make.height.mas_equalTo(height);
    }];
    //
    [self layoutSubviewsWithContentOffset:contentOffset Bili:bili contentViewHeight:height-Height_StatusBar];
}
/**
 icon 变化frame
 */
- (void)layoutSubviewsWithContentOffset:(CGPoint)contentOffset Bili:(CGFloat)bili contentViewHeight:(CGFloat)contenViewHeight {
    CGFloat fanBili = 1-bili;
    self.backgroundView.alpha = bili;
    self.serviceButton.customTitleLabel.alpha = self.prizeButton.customTitleLabel.alpha = fanBili;
    self.appNameLabel.alpha = fanBili;
    //更新icon以及附属视图
    CGSize iconSize = [self iconSize];
    //最大offset
    CGFloat iconMaxOffset = ((self.maxHeight-Height_StatusBar)/2)-iconSize.height/2;
    //iconCenterY的offset >= 0
    CGFloat iconCenterYOffset = iconMaxOffset*fanBili;
    //icon最小size
    CGSize iconMinSize = CGSizeMake(35, 35);
    //size伸缩范围
    CGFloat iconSizeChangeHeightValue = iconSize.height-iconMinSize.height;
    //
    CGFloat iconSizeChangeValue = iconSizeChangeHeightValue*bili;
    iconSize = CGSizeMake(iconSize.width-iconSizeChangeValue, iconSize.height-iconSizeChangeValue);
    //更新layout
    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).offset(iconCenterYOffset);
        make.size.mas_equalTo(iconSize);
    }];
    //更新regionButton
    CGFloat regionMaxOffset = ((self.maxHeight-Height_StatusBar)/2)-self.regionButton.titleLabel.font.lineHeight/2-1.0;
    //regionCenterYOffset的offset >= 0
    CGFloat regionCenterYOffset = regionMaxOffset*fanBili;
    [self.regionButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).offset(regionCenterYOffset);
    }];
    
}
- (void)displaySubviews:(BOOL)display {
    self.contentView.alpha = display?1:0;
    self.refreshView.alpha = self.contentView.alpha;
}

- (void)setFlowData:(BCHomeFlowModel *)flowData {
    _flowData = flowData;
    //
    if (flowData) {
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:flowData.icon] placeholderImage:LOAD_IMAGE(@"home_navi_logo")];
        //
        self.appNameLabel.text = flowData.cu_name;
        [self.regionButton setTitle:flowData.name forState:UIControlStateNormal];
        //
        self.serviceButton.customTitleLabel.text = flowData.right_2;
        self.prizeButton.customTitleLabel.text = flowData.right_1;
        //
        [self displaySubviews:true];
    }
}


#pragma mark - height
- (CGSize)iconSize {
    return CGSizeMake(52.0, 52.0);
}
- (CGFloat)iconOffset {
    return 20.0f;
}
//maxHeight
- (CGFloat)maxHeight {
    return Height_StatusBar+[self iconSize].height+[self iconOffset];
}
//minHeight
- (CGFloat)minHeight {
    return Height_StatusBar+44.0;
}
//拐点 及tableView的contentoffset在何时时 self 的高度不在变化
- (CGFloat)maxContentOffsetY {
    return 80.0f;
}


/**
 add actions
 */
- (void)addServiceButtonTarget:(id)target action:(SEL)action {
    [self.serviceButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
- (void)addPrizeButtonTarget:(id)traget action:(SEL)action {
    [self.prizeButton addTarget:traget action:action forControlEvents:UIControlEventTouchUpInside];
}
- (void)addRegionButtonTarget:(id)target action:(SEL)action {
    [self.regionButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}



/**
 将iconImageView的frame转换为toView的frame
 */
- (CGRect)convertIconImageViewFrameToView:(UIView *)toView {
    if (toView) {
        CGRect toSelfFrame = [self.contentView convertRect:self.iconImageView.frame toView:self];
        return [self convertRect:toSelfFrame toView:toView];
    }
    return CGRectZero;
}

@end
